package com.google.code.virtualhockey.vhx.server.model

import java.util.Date
import com.google.code.virtualhockey.vhx.sds.{STNumberedEntity, STMetaData}
import com.google.code.virtualhockey.vhx.sds.fields.{SDate, SShortText}
import scala.Some

/**
 * Represents a user history entry. Every change in the history of registered user is recorded as a history
 * item once a user changes something or in case the user cancels his registration.
 */
class RegisteredUserHistory( cpId: Option[Long],
                             cpHistoryReason: RegisteredUserHistoryReason.Value,
                             val userId: String,
                             val email: String,
                             val pseudonym: String,
                             val country: String,
                             val place: String,
                             val fromDate: Date,
                             val toDate: Date ) extends STNumberedEntity[RegisteredUserHistory] {
  
  setEntityId( cpId )

  private val _historyReason = cpHistoryReason.toString // persist enum as String
  def historyReason() = RegisteredUserHistoryReason.withName( _historyReason )

  /** Private no-arg constructor for SDS. */
  private def this() = this( None, null, null, null, null, null, null, null, null )
}

object RegisteredUserHistoryMeta extends STMetaData[RegisteredUserHistory] {
  val cls = classOf[RegisteredUserHistory]
  val kind = "RegisteredUserHistory"
  val fields = List(
    new SShortText( this, "_historyReason", true, Some( "historyReason" ) ),
    new SShortText( this, "userId", true ),
    new SShortText( this, "email", true ),
    new SShortText( this, "pseudonym", true ),
    new SShortText( this, "country", true ),
    new SShortText( this, "place", true ),
    new SDate( this, "fromDate", true ),
    new SDate( this, "toDate", true ) )

  /**
   * Creates a new Club instance from a CSV file line.
   */
  override def fromCsv( values: List[Any] ): RegisteredUserHistory = {
    new RegisteredUserHistory( getOptionalCsvValue( values(0) ),
                               RegisteredUserHistoryReason.withName( values(1).toString ),
                               values(2).toString,
                               values(3).toString,
                               values(4).toString,
                               values(5).toString,
                               values(6).toString,
                               values(7).asInstanceOf[Date],
                               values(8).asInstanceOf[Date] )
  }
}

/** Enumerates the possible reasons for the creation of a RegisteredUserHistory object. */
object RegisteredUserHistoryReason extends Enumeration {

  /** History entry created because some user registration data was updated. */
  val UPDATED = Value

  /** History entry created because the user registration was cancelled. */
  val CANCELLED = Value
}
